package com.server.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.server.database.SQLCreator;
import com.server.service.ICommonDataService;
import com.server.service.ISurveyAnalyzeService;
import com.server.web.DataVector;
import com.server.web.RequestInput;

/**
 * 
 * @author wufeng
 * @date 创建时间：2016年9月5日 下午3:30:16 
 */
@Service("SurveyAnalyzeService")
public class SurveyAnalyzeService implements ISurveyAnalyzeService{
	
	private static String tableName = "education.survey_distribute"; 
	
	@Autowired
	private ICommonDataService commonDataService;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public boolean saveMap(String operator, Map<String, Object> inputMap, String paperId,String uuid) {
		String field = "user_uuid = '"+uuid+"' and surveyId";
		boolean isSuccess = commonDataService.update(operator, tableName, field, paperId, inputMap);
		return isSuccess;
	}

	@Override
	public Map<String, Object> getMap(String paperId) {
		Map<String, Object> map = new HashMap<String, Object>();
		String sql = "select * from education.survey where id = '"+paperId+"'";
		String sql_1 = "select count(id) from education.survey_distribute where surveyId = '"+paperId+"'";
		String sql_2 = "select count(id) from education.survey_distribute where state = 1 and surveyId = '"+paperId+"'";
		String sql_3 = "select count(id) from education.message where isRead = 1 and relatedId = '"+paperId+"'";
		int scan,done,all; 
		try {
			List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
			if(list.size()>0){
				map = list.get(0);
			}
			all = jdbcTemplate.queryForObject(sql_1,Integer.class);
			done = jdbcTemplate.queryForObject(sql_2,Integer.class);
			scan = jdbcTemplate.queryForObject(sql_3,Integer.class);
			map.put("all", all);
			map.put("done", done);
			map.put("scan", scan);
		} catch (Exception e) {
			return new HashMap<String, Object>();
		}
		return map;		
	}

	@Override
	public Integer getState(String paperId, String uuid) {
		String sql = "select state from education.survey_distribute where surveyId = '"+paperId+"' and user_uuid = '"+uuid+"'";
		int state;
		try {
			state = jdbcTemplate.queryForObject(sql, Integer.class);
		} catch (Exception e) {
			state = -1;
		}
		return state;
	}

	@Override
	public List<Map<String, Object>> getTestList(String paperId, String type) {
		String sql = "select * from education.v_surveyPaper where survey_id = '"+paperId+"'"; 
		List<Map<String, Object>> reList = new ArrayList<Map<String,Object>>();
		try {
			reList = jdbcTemplate.queryForList(sql);
		} catch (Exception e) {
			return new ArrayList<Map<String,Object>>();
		}
		return reList;
	}

	@Override
	public List<Map<String, Object>> getAnswerList(String paperId) {
		String sql = "select * from education.survey_distribute where state = 1 and surveyId = '"+paperId+"'"; 
		List<Map<String, Object>> reList = new ArrayList<Map<String,Object>>();
		try {
			reList = jdbcTemplate.queryForList(sql);
		} catch (Exception e) {
			return new ArrayList<Map<String,Object>>();
		}
		return reList;
	}

	@Override
	public DataVector<Map<String, Object>> getSolutionAnswerList(
			String paperId, String order, RequestInput requestInput) {
		StringBuffer whereStr = new StringBuffer();
		String _whereStr = "surveyId = '"+paperId+"' and state = 1";
		String sortStr = "order by updateTime desc";
		if (!StringUtils.isEmpty(_whereStr)) {
			whereStr.append(_whereStr);
		}
		Integer pageIndex = 0;
		Integer pageSize = Integer.MAX_VALUE;
		if (requestInput != null) {
			if (requestInput.getWhereStr() != null
					&& requestInput.getWhereStr().length() > 0) {
				whereStr.append("(").append(requestInput.getWhereStr())
						.append(")");
			}
			if (requestInput.getSortStr() != null
					&& requestInput.getSortStr().length() > 0) {
				sortStr = requestInput.getSortStr();
			}
			pageIndex = requestInput.getPageIndex();
			pageSize = requestInput.getPageSize();
		}
		String sqlCount = SQLCreator.getSqlCount(tableName, whereStr.toString(),
				null, null);
		String sql = SQLCreator.getSql_Mysql(pageIndex, pageSize, tableName,
				whereStr.toString(), null, null, null);
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		for (Map<String, Object> map : list) {
			Object object = map.get("answer");
			String answers[] = new String[1000];
			if(!StringUtils.isEmpty(object)){
				answers = object.toString().split("_kahsdkf_fsd!~&\\*_");
				map.put("solution", answers[Integer.parseInt(order)-1]);
			}
		}
		Integer count = jdbcTemplate.queryForObject(sqlCount, Integer.class);
		return new DataVector<Map<String, Object>>(list, count);
	}
	 
	
}
